﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class CompanySortController : BenefitHelper.Tools.BaseControl
    {
        //
        // GET: /CompanySort/

        public ActionResult Index(int uid, string pdate)
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            ViewBag.datequery = cookie.GetCookie("querydate");
            int operId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            if (BenefitHelper.Sys.Company.HasInited(db))
            {
                ViewBag.company = db.Company.First();
            }
            else
            {
                ViewBag.company = new BenefitHelper.Sys.Company();
            }
            if (BenefitHelper.Sys.VisitPrower.HasCompanyPrower(operId, db))
            {

                if (pdate.Length > 4)//查询月数据
                {
                    ViewBag.queryyear = pdate.Substring(0, 4);
                    ViewBag.querymonth = pdate.Substring(4, 2);
                    //  ViewBag.queryday = pdate.Substring(6, 2);
                    ViewBag.pdate = pdate;
                    int year = Convert.ToInt32(pdate.Substring(0, 4));
                    int month = Convert.ToInt32(pdate.Substring(4, 2));
                    //    int day = Convert.ToInt32(pdate.Substring(6, 2));
                    
                    BenefitHelper.Data.Month.GroupMonthSum gms = new BenefitHelper.Data.Month.GroupMonthSum();
                    var lists = gms.GetGroupMonthSum(month, year, db).OrderByDescending(a => a.Profit);
                    ViewBag.list = lists;

                    BenefitHelper.Data.Month.BrokerageMonthTradeInfo info = new BenefitHelper.Data.Month.BrokerageMonthTradeInfo();
                    var brokerageinfos = info.GetBrokerageMonthTradeInfo(year, month, db);
                    ViewBag.brokerageinfos = brokerageinfos;
                }
                else//查询年数据
                {
                    ViewBag.queryyear = pdate.Substring(0, 4);
                    ViewBag.querymonth = "";
                    //  ViewBag.queryday = pdate.Substring(6, 2);
                    ViewBag.pdate = pdate;
                    int year = Convert.ToInt32(pdate.Substring(0, 4));
                    // int month = Convert.ToInt32(pdate.Substring(4, 2));
                    //    int day = Convert.ToInt32(pdate.Substring(6, 2));

                    BenefitHelper.Data.Year.GroupYearSum gms = new BenefitHelper.Data.Year.GroupYearSum();
                    var lists = gms.GetGroupYearSum(year, db).OrderByDescending(a => a.Profit);
                    ViewBag.list = lists;

                    BenefitHelper.Data.Year.BrokerageYearTradeInfo info = new BenefitHelper.Data.Year.BrokerageYearTradeInfo();
                    var brokerageinfos = info.GetBrokerageYearTradeInfo(year, db);
                    ViewBag.brokerageinfos = brokerageinfos;
                }
                return View();
            }
            else
            {
                db.Dispose();
                return Redirect("/Error/HasNoPrower/" + uid + "/" + pdate);
            }
        }

    }
}
